package com.mes.license_demo;

import com.mes.license_demo.license.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: ZZB
 * @Description:
 * @Date: create in 2020/8/20 14:23
 */

//@Component
//public class LicenseCreatorController implements CommandLineRunner {
@RestController
public class LicenseCreatorController{

    // 证书subject
    @Value("${license.subject}")
    private String subject;

    // 公钥别称
    @Value("${license.publicAlias}")
    private String publicAlias;

    // 访问公钥库的密码
    @Value("${license.storePass}")
    private String storePass;
    // 访问公钥库的密码
    @Value("${license.keyPass}")
    private String keyPass;

    // 证书生成路径
    @Value("${license.licensePath}")
    private String licensePath;

    // 密钥库存储路径
    @Value("${license.publicKeysStorePath}")
    private String publicKeysStorePath;

    @Value("${license.check}")
    private boolean check;

    // 密钥库存储路径
    @Value("${license.privateKeysPath}")
    private String privateKeysPath;
    // 密钥库存储路径
    @Value("${license.licenseYears}")
    private Integer licenseYears;


    /**
     * 生成证书
     * @author zifangsky
     * @date 2018/4/26 13:13
     * @since 1.0.0
     * @param param 生成证书需要的参数，如：{"subject":"ccx-models","privateAlias":"privateKey","keyPass":"5T7Zz5Y0dJFcqTxvzkH5LDGJJSGMzQ","storePass":"3538cef8e7","licensePath":"C:/Users/zifangsky/Desktop/license.lic","privateKeysStorePath":"C:/Users/zifangsky/Desktop/privateKeys.keystore","issuedTime":"2018-04-26 14:48:12","expiryTime":"2018-12-31 00:00:00","consumerType":"User","consumerAmount":1,"description":"这是证书描述信息","licenseCheckModel":{"ipAddress":["192.168.245.1","10.0.5.22"],"macAddress":["00-50-56-C0-00-01","50-7B-9D-F9-18-41"],"cpuSerial":"BFEBFBFF000406E3","mainBoardSerial":"L1HF65E00X9"}}
     * @return java.util.Map<java.lang.String,java.lang.Object>
     */
    @RequestMapping(value = "/generateLicense",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Object generateLicense() throws ParseException {
        //
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        LicenseCreatorParam param=new LicenseCreatorParam();
        param.setSubject(subject);
        param.setPrivateAlias("privateKey");
        param.setKeyPass(keyPass);
        param.setStorePass(storePass);
        param.setConsumerType("user");
        param.setConsumerAmount(1);
        param.setPrivateKeysStorePath(privateKeysPath);
        param.setLicensePath(licensePath);
        String curDate = TimeUtil.getCurDate();
        param.setIssuedTime(simpleDateFormat.parse(curDate));
        param.setExpiryTime(simpleDateFormat.parse(TimeUtil.getNextYear(licenseYears)));
//        param.setDescription("这是证书描述信息");
        //
        if(StringUtils.isBlank(param.getLicensePath())){
            param.setLicensePath(licensePath);
        }
        LicenseCheckModel serverInfos = getServerInfos();
        param.setLicenseCheckModel(serverInfos);

        LicenseCreator licenseCreator = new LicenseCreator(param);
        boolean result = licenseCreator.generateLicense();


        return result;
    }

    private LicenseCheckModel getServerInfos() {
        // 操作系统类型
        String osName = System.getProperty("os.name").toLowerCase();
        AbstractServerInfos abstractServerInfos;
        // 根据不同操作系统类型选择不同的数据获取方法
        if (osName.startsWith("windows")) {
            abstractServerInfos = new WindowsServerInfos();
        } else if (osName.startsWith("linux")) {
            abstractServerInfos = new LinuxServerInfos();
        } else {//其他服务器类型
            abstractServerInfos = new LinuxServerInfos();
        }
        return abstractServerInfos.getServerInfos();
    }

}

